﻿namespace Profit.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using Core.Base;
    using Core.Contracts.Interfaces;

    /// <summary>
    /// Interaction logic for RobotControl.xaml
    /// </summary>
    public partial class RobotControl : UserControl
    {
        /// <summary>
        /// Selected robot property
        /// </summary>
        public static DependencyProperty RobotProperty = DependencyProperty.Register("Robot", typeof(RobotBase), typeof(RobotControl), new PropertyMetadata((RobotBase)null));
        /// <summary>
        /// Remove module command handler
        /// </summary>
        public static DependencyProperty RemoveModuleCommandProperty = DependencyProperty.Register("RemoveModuleCommand", typeof(ICommand), typeof(RobotControl), new PropertyMetadata((ICommand)null));
        /// <summary>
        /// Add module command handler
        /// </summary>
        public static DependencyProperty AddModuleCommandProperty = DependencyProperty.Register("AddModuleCommand", typeof(ICommand), typeof(RobotControl), new PropertyMetadata((ICommand)null));
        

        public RobotControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Obtains or defines the current selected robot
        /// </summary>
        public RobotBase Robot
        {
            get { return GetValue(RobotProperty) as RobotBase; }
            set { SetValue(RobotProperty,value); }
        }
        /// <summary>
        /// Obtains or defines the AddModuleCommand ICommand handler
        /// </summary>
        public ICommand AddModuleCommand
        {
            get { return GetValue(AddModuleCommandProperty) as ICommand; }
            set { SetValue(AddModuleCommandProperty, value); }
        }
        /// <summary>
        /// Obtains or defines the RemoveModuleCommand ICommand handler
        /// </summary>
        public ICommand RemoveModuleCommand
        {
            get { return GetValue(RemoveModuleCommandProperty) as ICommand; }
            set { SetValue(RemoveModuleCommandProperty, value); }
        }

        protected override void OnDragEnter(DragEventArgs e)
        {
            e.Handled = true;
            base.OnDragEnter(e);
            if (!e.Data.GetDataPresent(typeof(string)))
                e.Effects = DragDropEffects.None;
        }

        protected override void OnDrop(DragEventArgs e)
        {
            e.Handled = true;
            base.OnDrop(e);
            InvokeAddModule(e.Data.GetData(typeof (string)) as string);
        }

        private void MenuItemAmmunitionClick(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            
            var ammo = ((MenuItem)e.OriginalSource).Header as IAmmunition;
            if(ammo == null) return;

            var module = ((MenuItem) e.Source).CommandParameter as IModule;
            if(module ==null) return;

            if (!Equals(module.Ammunitions, ammo))
                module.Ammunitions = ammo;
        }

        private void MenuItemRemoveModuleClick(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            var menu = sender as MenuItem;
            if(menu ==null || menu.CommandParameter as ModuleBase ==null) return;

            InvokeRemoveModule((ModuleBase) menu.CommandParameter);
        }

        private void ListViewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            var lv = sender as ListView;
            if (lv == null || lv.SelectedItem == null || lv.SelectedItem as ISlot == null) return;

            InvokeRemoveModule(((ISlot)lv.SelectedItem).FittedModule);
        }

        private void InvokeAddModule(string module)
        {
            if (AddModuleCommand != null && AddModuleCommand.CanExecute(module))
                AddModuleCommand.Execute(module);
        }

        private void InvokeRemoveModule(IModule module)
        {
            if (RemoveModuleCommand != null && RemoveModuleCommand.CanExecute(module))
                RemoveModuleCommand.Execute(module);
        }

    }
}
